package org.gongliang.jsmile.authc.sys.service;

import java.util.List;

import org.gongliang.jsmile.authc.sys.dao.SysRoleDao;
import org.gongliang.jsmile.authc.sys.entity.SysRole;
import org.gongliang.jsmile.authc.sys.entity.SysRoleResources;
import org.gongliang.jsmile.common.base.CRUDService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
* 
* @author 龚亮
* @version 1.0
* @date 2018年08月25日 04:57:24
*/
@Service
public class SysRoleService extends CRUDService<SysRoleDao, SysRole> {


	@Autowired
	private SysRoleDao roleDao;

	/**
	 * 
	 * @Description 查询用户所拥有的角色
	 * @author 龚亮
	 * @param userId
	 *            用户id
	 * @return 角色集合
	 */
	@Transactional(readOnly = true, rollbackFor = Exception.class)
	public List<SysRole> getRoleByUser(Integer userId) {
		return roleDao.getRoleByUser(userId);
	}




	/**
	 * desc: 批量角色删除
	 * @param ids
	 * @return
	 */
	@Transactional(readOnly = false, rollbackFor = Exception.class)
	public boolean deleteRoles(String[] ids) {
		int flag=0;
		for(int i=0;i<ids.length;i++) {
			flag+=roleDao.deleteRoleResource(Integer.parseInt(ids[i]));
		}
		flag+=roleDao.deleteByIds(ids);
		return retBool(flag);
	}
	/**
	 * desc: 添加角色权限
	 * @param ids
	 * @return
	 */
	@Transactional(readOnly = false, rollbackFor = Exception.class)
	public boolean saveRoleResources(Integer roleId, List<Integer> resourceIds) {
		int i = roleDao.deleteRoleResource(roleId);
		if (resourceIds != null&& resourceIds.size() > 0) {
			for (Integer resourceId : resourceIds) {
					SysRoleResources rr = new SysRoleResources();
					rr.setResourcesId(resourceId);
					rr.setRoleId(roleId);
					i+=roleDao.saveRoleResources(rr);
			}
		} 
		return retBool(i);
	}

}
